#ifndef __RWLOCK_H
#define __RWLOCK_H
#include "stdtype.h"
#include "spinlock.h"
#include "dlist.h"

typedef struct
{
    volatile mx_usize_t wf_rc;          // write flag of read counter
    mx_dlist_pt suspend;                // the thread list that wait for rwlock to be leave
    mx_spinlock_t lock;                 // the lock for access the descriptor
} mx_rwlock_t, *mx_rwlock_pt;

/* initialize the rwlock */
void mx_rwlock_init (mx_rwlock_pt prwlock);
/* get the read lock */
void mx_rwlock_rlock (mx_rwlock_pt prwlock);
/* try to get the read lock */
mx_bool_t mx_rwlock_tryrlock (mx_rwlock_pt prwlock);
/* get the write lock */
void mx_rwlock_wlock (mx_rwlock_pt prwlock);
/* try to get the write lock */
mx_bool_t mx_rwlock_trywlock (mx_rwlock_pt prwlock);
/* unlock the rwlock, both rlock and wlock can call this function to unlock */
void mx_rwlock_unlock (mx_rwlock_pt prwlock);

#endif
